﻿using System;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;

namespace iTeamyou.LTS.MigrationTool
{
    public class MigrationProvider
    {
        private readonly IMongoCollection<MangoSKU>? skuList;
        private readonly IMongoCollection<MangoProvider>? supplierList;
        private readonly IMongoCollection<MangoChannelSKU>? channelList;
        private readonly IMongoCollection<MangoStockStore>? batchList;
        private readonly IMongoCollection<MangoResource>? resourceList;
        private readonly IMongoCollection<MangoCert>? certList;
        public string ConnectionString;
        public string DatabaseName;
        public string ProductCollectionName;
        public string SupplierCollectionName;
        public string ChannelCollectionName;
        public string BatchCollectionName;
        public string ResourceCollectionName;
        public string CertGroupCollectionName;

        public MigrationProvider()
        {
            var configurationBuilder = new ConfigurationBuilder()
                                    .SetBasePath(Directory.GetCurrentDirectory())
                                    .AddJsonFile("appsettings.json", false, reloadOnChange: true);

            var configuration = configurationBuilder.Build();
            ConnectionString = configuration["MongoDBSettings:ConnectionString"];
            DatabaseName = configuration["MongoDBSettings:DatabaseName"];
            ProductCollectionName = configuration["MongoDBSettings:ProductCollectionName"];
            SupplierCollectionName = configuration["MongoDBSettings:SupplierCollectionName"];
            ChannelCollectionName = configuration["MongoDBSettings:ChannelCollectionName"];
            BatchCollectionName = configuration["MongoDBSettings:BatchCollectionName"];
            ResourceCollectionName = configuration["MongoDBSettings:ResourceCollectionName"];
            CertGroupCollectionName = configuration["MongoDBSettings:CertGroupCollectionName"];

            var client = new MongoClient(ConnectionString);
            var db = client.GetDatabase(DatabaseName);

            skuList = db.GetCollection<MangoSKU>(ProductCollectionName);
            supplierList = db.GetCollection<MangoProvider>(SupplierCollectionName);
            channelList = db.GetCollection<MangoChannelSKU>(ChannelCollectionName);
            batchList = db.GetCollection<MangoStockStore>(BatchCollectionName);
            resourceList = db.GetCollection<MangoResource>(ResourceCollectionName);
            certList = db.GetCollection<MangoCert>(CertGroupCollectionName);
        }

        public List<MangoSKU> GetSKU()
        {
            return skuList.Find(_ => true).ToList();
        }

        public MangoSKU GetSKUByBarcode(string barcode)
        {
            return skuList.Find(a => a.barcode == barcode).ToList().FirstOrDefault();
        }

        public MangoProvider GetSupplierByCode(string code)
        {
            return supplierList.Find(s => s.type == 3 && s.code == code).ToList().FirstOrDefault();
        }

        public MangoProvider GetManufacturerByCode(string code)
        {
            return supplierList.Find(s => s.code == code && (s.type == 1 || s.type == 2)).ToList().FirstOrDefault();
        }

        public MangoStockStore GetBatchByCode(string code)
        {
            return batchList.Find(s => s.id == long.Parse(code)).ToList().FirstOrDefault();
        }

        public List<MangoProvider> GetSupplier()
        {
            return supplierList.Find(s => s.type == 3).ToList();
            //return supplierList.Find(_ => true).ToList();
        }

        public List<MangoChannelSKU> GetChannels()
        {
            return channelList.Find(_ => true).ToList();
        }

        public List<MangoProvider> GetManufacturer()
        {
            return supplierList.Find(s => s.type == 1 || s.type == 2).ToList();
        }

        public List<MangoStockStore> GetBatch()
        {
            return batchList.Find(_ => true).ToList();
        }

        public List<MangoCert> GetProductCert()
        {
            return certList.Find(s => s.subject_type == 2 && s.extend_info != "[]").ToList();
        }

        public List<MangoCert> GetProviderCert()
        {
            return certList.Find(s => s.subject_type == 1 && s.extend_info != "[]").ToList();
        }

        public List<MangoCert> GetBatchCert()
        {
            return certList.Find(s => s.subject_type == 3 && s.extend_info != "[]").ToList();
        }

        public MangoResource GetResourcesById(string id)
        {
            return resourceList.Find(r => r.id == long.Parse(id) && r.is_delete == 0).FirstOrDefault();
        }
    }
}

